<template>
	<view class="pres">
		<view class="btn-box">
			<m-button radius="10" block  openType="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号一键登录</m-button>
			<m-button radius="10" spa="30 0 0" block hollow @click="Jump(2)">暂不登录</m-button>
			
		</view>
	</view>
</template>

<script>
	import { UserInfo, wxRegist, BindPhone, bindInvcode, SetDistrict } from '@/api/Users.js'
	import { GetCoinConfig } from '@/api/Ctrls.js'
	import Updater from '@/utils/upgrade.js'
	import { isEmpty } from 'libs/fn/verify.js'
	import { mapState } from 'vuex'
	export default {
		data() {
			return {
				macno: '',
				status: '',
				checkUpLoading: true,
				guideStatus: true,
				//绑定邀请人
				blindLoading: false,
				blindShow: false,
				invcode: '',

				userinfos: {},
				devCode: '',
			};
		},
		onLoad(res) {
			const { status, invcode, q } = res
			this.status = status;

			this.invcode = invcode || this.Cookie.get('Invcode');
		},
		computed: {
			...mapState({
				location: state => state.address.location,
			}),
		},
		onShow() {
			this.wxRegist()
		},
		methods: {
			async getPhoneNumber(data) {
				try {
					const { detail: { encryptedData, iv, code } } = data;
					uni.showLoading({
						title: "登录中……"
					})

					const login = await uni.login({ provider: 'weixin' });
					console.log('wxCode', login.code)
					console.log('phoneCode', code)
					//获取OpenId
					const { msg, success, data: userdata } = await wxRegist({
						'authcode': code,
						'wxCode': login.code,
						'invcode': this.invcode
					})



					// const { msg, success, data: userdata } = await BindPhone({
					// 	authcode: code,
					// 	openid_authcode: login.code,
					// 	invcode: this.Cookie.get('Invcode')
					// })
					uni.hideLoading()
					this.Toast(success ? '登录成功' : msg)
					if (success) {




						await this.setLocation();




						if (this.status == 'login') {
							return this.Jump()
						}
						this.Jump({
							url: "/pages/home/home",
							mode: 4,
						});
					}

				} catch (e) {
					uni.hideLoading()
					this.Toast(e)
					console.log(e)
					//TODO handle the exception
				}
			},
			async setLocation() {
				console.log('setLocation',this.location)
				if(this.location?.country){
					const { country, district, province, city } = this.location
					await SetDistrict({
						province: province,
						city: city || province,
						district: district,
					})
				}
			},

			async wxRegist() {
				if (this.status != 'update') {
					const { success, data } = await UserInfo(false)
					if (success) {
						this.userinfos = data;
					}
				}

				// return  this.Jump({
				// 	url:'/pagesA/error',
				// 	mode:3
				// })



				// const { code } = await uni.login({ provider: 'weixin' });
				// console.log('wxCode', code)
				// this.wxCode = code;
				// //获取OpenId
				// const res = await wxRegist({
				// 	'authcode': code,
				// 	'invcode': this.invcode
				// })
				// let { success, data } = res;
				// //登录失败不执行
				// if (!success) return
				// this.userinfos = data;
				// console.log('个人信息', data)
			},
			async bindInvcode() {
				if (!this.invcode) {
					return this.Toast('请输入手机号或邀请码')
				}
				this.blindLoading = true;
				const { success, msg } = await bindInvcode({
					invcode: this.invcode,
				})
				this.blindLoading = false;
				this.Toast(msg)
				if (success) {
					this.nextStep();
				}
			},
		}
	}
</script>

<style lang="scss">
	.pres {
		height: 100vh;
		background: url('http://117.24.6.18:5003/static/tzimg/pres-bg.png') #fff no-repeat;
		background-size: 100% auto;
		background-position: center;
		position: relative;

	}

	.btn-box {
		width: 100%;
		padding: 0 24rpx;
		position: fixed;
		bottom: 10%;
		box-sizing: border-box;
		// display: grid;
		// grid-template-columns: 1fr 1fr;
		// grid-gap: 30rpx;
		margin: 100rpx 0rpx 90rpx;
	}

	.getPhone {

		width: 100%;

		display: inline-block;
		border-width: 0.5px;
		border-style: solid;
		border-color: var(--theme);
		border-image: initial;
		font-size: 26rpx;
		font-family: "PingFang SC", "PingFang SC";
		padding: 20rpx 34rpx;
		border-radius: 10rpx;
		background: var(--theme);
		color: #fff;
		line-height: unset !important;

		&::after {
			display: none;
		}

	}

	button[disabled]:not([type]) {
		background: var(--theme) !important;
		cursor: not-allowed !important;
		color: #fff !important;
		opacity: 0.7;

		&:before {
			display: none !important;
		}
	}




	.setName-box {
		padding: 24rpx 0;

		.form-Item {
			background: #f6f6f6;
			border-radius: 15rpx;
			box-sizing: border-box;

			input {
				border-radius: 15rpx;
				padding: 20rpx 28rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #111;

			}
		}

		.hint {
			padding: 24rpx 0 0;
			font-size: 24rpx;
			color: var(--text_1);
		}
	}
</style>